using System.Collections;

public class WaitCoroutine : WaitInstruction {
	IEnumerator _iterator;
	public WaitCoroutine(ShipProgram program, IEnumerator iterator) : base(program) {
		_iterator = iterator;
	}

	public override bool Check() {
		if ( _iterator == null ) {
			return true;
		}
		if ( !(_iterator.Current is WaitInstruction) ) {
			var canContinue = _iterator.MoveNext();
			if ( !canContinue ) {
				_iterator = null;
				return true;
			}
			return false;
		}
		var instruction = (WaitInstruction)_iterator.Current;
		if ( instruction == null ) {
			return true;
		}
		var shouldContinue = instruction.Check();
		if ( shouldContinue ) {
			if ( !_iterator.MoveNext() ) {
				_iterator = null;
				return true;
			}
		}
		return false;
	}
}
